package com.yijiaw.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.yijiaw.dao.ProductDAO;
import com.yijiaw.domain.Product;
import com.yijiaw.domain.Producttype;
import com.yijiaw.service.ProductService;
import com.yijiaw.service.ProducttypeService;
import com.yijiaw.util.FileUploadUtil;

@Controller
@RequestMapping("/admin")
public class ProductController {

	@Autowired
	ProductDAO productDAO;
	
	@Autowired
	ProductService productService;
	
	@Autowired
	ProducttypeService producttypeService;
	
	@Autowired
	FileUploadUtil fileUploadUtil;
	
	/**
	 * 查看产品列表
	 * @param model
	 * @param type
	 * @return
	 */
	@RequestMapping({"/produ/hanger", "produ"})
	public String produList(Model model) {
		
		List<Product> list = productService.findAllProducts(0, productService.countProducts());
		model.addAttribute("list", list);
		model.addAttribute("title", "产品中心");
		model.addAttribute("target", "produ");
		
		return "admin/tab";
	}
	
	/**
	 * 查看单个产品
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/produ/view/{id}")
	public String view(@PathVariable int id, Model model) {
		Product product = productService.findProductByPrimaryKey(id);
		List<Producttype> list = producttypeService.findAllProducttypes(0, producttypeService.countProducttypes());
		
		model.addAttribute("product", product);
		model.addAttribute("list", list);
		model.addAttribute("target", "../update");
		
		return "admin/view";
	}
	
	/**
	 * 删除产品
	 * @param id
	 * @return
	 */
	@RequestMapping("/produ/delete/{id}")
	public String delete(@PathVariable int id) {
		Product product = productService.findProductByPrimaryKey(id);
		if (product != null) {
			productService.deleteProduct(product);
		}
		
		return "redirect:../hanger";
	}
	
	/**
	 * 显示添加产品表单
	 * @return
	 */
	@RequestMapping(value = "/produ/add", method = RequestMethod.GET)
	public String add(Model model) {
		List<Producttype> list = producttypeService.findAllProducttypes(0, producttypeService.countProducttypes());
		model.addAttribute("list", list);
		model.addAttribute("target", "add");
		
		return "admin/view";
	}
	
	/**
	 * 添加产品
	 * @param product
	 * @param type
	 * @param file
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/produ/add", method = RequestMethod.POST)
	public String add(@ModelAttribute Product product, @RequestParam("type") int type, @RequestParam("file") MultipartFile file, @RequestParam("file2") MultipartFile file2, HttpServletRequest request) throws Exception {
		
		Producttype producttype = producttypeService.findProducttypeByPrimaryKey(type);
		product.setProducttype(producttype);
		String path = request.getSession().getServletContext().getRealPath("upload/images/");
		/**
		 * 处理上传图片，统一上传到网站根目录下的upload/images目录下
		 */
		if (file.getSize() > 0) {
			product.setImage(fileUploadUtil.write(file, path));
		}
		if (file2.getSize() > 0) {
			product.setBigImage(fileUploadUtil.write(file2, path));
		}
		product.setRemark("<pre>" + product.getRemark() + "</pre>");
		productService.saveProduct(product);
		
		return "redirect:hanger";
	}
	
	/**
	 * 更新产品
	 * @param product
	 * @param type
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping("/produ/update")
	public String update(@ModelAttribute Product product, @RequestParam("type") int type, @RequestParam("file") MultipartFile file, @RequestParam("file2") MultipartFile file2, HttpServletRequest request) {
		String path = request.getSession().getServletContext().getRealPath("upload/images/");
		if (file.getSize() > 0) {
			product.setImage(fileUploadUtil.write(file, path));
		}
		if (file2.getSize() > 0) {
			product.setBigImage(fileUploadUtil.write(file2, path));
		}
		product.setRemark("<pre>" + product.getRemark() + "</pre>");
		productService.saveProduct(product);
		productService.saveProductProducttype(product.getId(), producttypeService.findProducttypeByPrimaryKey(type));
		
		return "redirect:hanger";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
